<?php

namespace App\Models;

use Dcat\Admin\Traits\HasDateTimeFormatter;

use Illuminate\Database\Eloquent\Model;
use Spatie\EloquentSortable\Sortable;
use Spatie\EloquentSortable\SortableTrait;
class Team extends Model implements Sortable
{
    use HasDateTimeFormatter,SortableTrait;

    protected $casts = [
        'banners'=>'json',
    ];

    public function getPictureAttribute($value)
    {
        return json_decode($value,true);
    }

    public function getEnvironmentPicturesAttribute($value)
    {
        return json_decode($value,true);
    }

    protected $sortable = [
        // 设置排序字段名称
        'order_column_name' => 'sort',
        // 是否在创建时自动排序，此参数建议设置为true
        'sort_when_creating' => true,
    ];

    public function products()
    {
        return $this->hasMany(Product::class);
    }

    public function popular($limit = 5){
        return $this->products->sortByDesc('price')->take($limit);
    }

}
